package com.etime.shycourse.controller.pay;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.bouncycastle.cms.PasswordRecipientInfoGenerator;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.*;

//@Api(tags = "微信小程序支付")
//@RestController
//@RequestMapping("/wx/pay")
public class WXPay {

//    @ApiOperation("微信小程序支付接口")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "orderNo",value = "订单号"),
//            @ApiImplicitParam(name="orderPrice",value = "价格"),
//            @ApiImplicitParam(name="productName",value = "商品名称"),
//            @ApiImplicitParam(name="openId",value = "openid"),
//            @ApiImplicitParam(name="remark",value = "备注")
//    })
//    @RequestMapping("/program")
//    public String wxprogrampay(@RequestBody WXProgramPay payentity){
//        Map<String, Object> paramMap = new HashMap<String, Object>();
//        paramMap.put ("orderPrice", payentity.getOrderPrice());
//        paramMap.put ("payWayCode", "WEIXIN");// 支付方式编码 支付宝: ALIPAY  微信:WEIXIN
//        paramMap.put ("orderNo", payentity.getOrderNo());
//        Date orderDate = new Date ();//订单日期
//        String orderDateStr = new SimpleDateFormat("yyyyMMdd").format (orderDate);// 订单日期
//        paramMap.put ("orderDate", orderDateStr);
//        Date orderTime = new Date ();//订单时间
//        String orderTimeStr = new SimpleDateFormat ("yyyyMMddHHmmss").format (orderTime);// 订单时间
//        paramMap.put ("orderTime", orderTimeStr);
//        paramMap.put ("payKey", "a6e308fa192c4b2ea0a8fa8621b083cb");
//        paramMap.put ("productName", payentity.getProductName());
//        String notifyUrl = "https://"; // 后台消息通知Url
//        paramMap.put ("notifyUrl", notifyUrl);
//        paramMap.put ("openId", payentity.getOpenId());
//        paramMap.put ("orderIp", "127.0.0.1");
//        paramMap.put ("remark", payentity.getRemark());
//        paramMap.put("payType","WX_PROGRAM_PAY");
//        /////签名及生成请求API的方法///
//        String sign = MerchantApiUtil.getSign (paramMap, "etime30060090aET91510100MA62Q6YD");
//        paramMap.put ("sign", sign);
//        HttpClient httpClient = HttpClients.createDefault ();
//        HttpPost post = new HttpPost ("http://139.155.183.118:9000/gateway/programpay/doPay");
//        try {
//            List<NameValuePair> list = new ArrayList<>();
//            for (Map.Entry<String, Object> entry : paramMap.entrySet ()) {
//                list.add (new BasicNameValuePair(entry.getKey (), String.valueOf (entry.getValue ())));
//            }
//            post.setEntity (new UrlEncodedFormEntity(list, "UTF-8"));
//            HttpResponse httpResponse = httpClient.execute (post);
//            String result = EntityUtils.toString (httpResponse.getEntity ());
//            System.out.println (result);
//            return result;
//        } catch (Exception e) {
//            e.printStackTrace ();
//        }
//        return "";
//    }

//    @PostMapping("/notice")
//    public String wxNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        Map orderMap = new HashMap();
//        BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream()));
//        String line = null;
//        StringBuilder sb = new StringBuilder();
//        while ((line = br.readLine()) != null) {
//            sb.append(line);
//        }
//        String notityXml = sb.toString();
//        String resXml = "";
//        Map resPrint = new HashMap();
//        Map<String, String> resultMap = WXPayUtil.xmlToMap(notityXml);
//        String returnCode = (String) resultMap.get("return_code");//业务结果
//        String orderNo = resultMap.get("out_trade_no");//订单号
////        String sign = resultMap.get("sign");//获取微信签名
////        resultMap.remove("sign");//去除签名字段
////        String signNew = WXPayUtil.generateSignature(resultMap, WeChatConfig.WECHAT_key); //重新签名
////        if (signNew.equals(sign)) {
////            if ("SUCCESS".equals(returnCode)) {
////                System.out.println(signNew + "ppppp");
////                resPrint.put("return_code", "SUCCESS");
////                resPrint.put("return_msg", "ok");
////                resXml = WXPayUtil.mapToXml(resPrint);
////                orderMap.put("orderStatus", 1);
////                orderMap.put("orderNo", orderNo);
////                // 自己的业务逻辑
//
//                return WxPayNotifyResponse.success("成功");
//            } else {
//                System.out.println("业务结果失败");
//                return null;//WxPayNotifyResponse.success("code:" + 9999 + "微信回调结果异常,异常原因:");
//            }
//
//        } else {
//            resPrint.put("return_code", "FAIL");
//            resPrint.put("return_msg", "签名失败");
//            resXml = WXPayUtil.mapToXml(resPrint);
//        }
//        log.info(resXml);
//        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
//        out.write(resXml.getBytes());
//        out.flush();
//        out.close();
//        br.close();
//        return null;
//    }



}
